package com.holike.crm.fragment.homepage;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.holike.crm.R;
import com.holike.crm.base.MyFragment;
import com.holike.crm.base.ToolbarHelper;
import com.holike.crm.bean.OrderDetailsBean;
import com.holike.crm.fragment.customer.LogisticsInfoFragment;
import com.holike.crm.fragment.customer.OperationLogFragment;
import com.holike.crm.fragment.customer.ProductInfoFragment;
import com.holike.crm.fragment.customer.QuotationListNewFragment;
import com.holike.crm.fragment.customer.ReceiptDeliveryManifestFragment;
import com.holike.crm.fragment.customer.SpaceManifestFragment;
import com.holike.crm.popupwindown.ListMenuPopupWindow;
import com.holike.crm.presenter.activity.OrderDetailsPresenter;
import com.holike.crm.util.Constants;
import com.holike.crm.view.activity.OrderDetailsView;
import com.umeng.analytics.MobclickAgent;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


/*订单详情*/
public class OrderDetailsFragment extends MyFragment<OrderDetailsPresenter, OrderDetailsView> implements ListMenuPopupWindow.ItemSelectListener, OrderDetailsView {

    private FrameLayout mContainer;
    private String mOrderId = "", mMessageId = "";

    @Override
    protected OrderDetailsPresenter attachPresenter() {
        return new OrderDetailsPresenter();
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_order_details;
    }

    @Override
    protected void init() {
        super.init();
        MobclickAgent.onEvent(mContext, "order_details");
        setTitle(mContext.getString(R.string.order_details_title));
        setOptionsMenu(R.menu.menu_more);
        mContainer = mContentView.findViewById(R.id.fl_container);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mOrderId = bundle.getString(Constants.ORDER_ID, "");
            mMessageId = bundle.getString(Constants.MESSAGE_ID, "");
        }
        initData();
    }

    private void initData() {
        showLoading();
        mPresenter.getOrderDetails(mOrderId, mMessageId);
    }

    @Override
    public void onOptionsMenuClick(Toolbar toolbar, MenuItem menuItem) {

        ListMenuPopupWindow popupWindow = new ListMenuPopupWindow(mContext, R.array.title_order_info, R.array.title_order_info_id, this);
//        popupWindow.showAsDropDown(actionView, -DensityUtil.dp2px(6), 0, Gravity.END);
        ToolbarHelper.showPopupWindow(popupWindow, toolbar);
    }

    @Override
    public void onSelect(String title) {
        Map<String, Serializable> params = new HashMap<>();
        params.put(Constants.ORDER_ID, mOrderId);
        switch (title) {
            case "物流信息":
                startFragment(params, new LogisticsInfoFragment());
                break;
            case "空间清单":
                startFragment(params, new SpaceManifestFragment());
                break;
            case "收发货清单":
                startFragment(params, new ReceiptDeliveryManifestFragment());
                break;
            case "报价清单":
                startFragment(params, new QuotationListNewFragment());
                break;
            case "产品信息":
                startFragment(params, new ProductInfoFragment());
                break;
            case "操作日志":
                startFragment(params, new OperationLogFragment());
                break;
        }
    }

    @Override
    public void getDetailsSuccess(OrderDetailsBean orderDetailsBean) {
        dismissLoading();
        mContainer.removeAllViews();
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.include_order_detail_content, mContainer, true);
        setText(R.id.tv_order_details_order_number,TextUtils.isEmpty(orderDetailsBean.getOrderId()) ? "-" : orderDetailsBean.getOrderId());
        setText(R.id.tv_order_details_order_type,TextUtils.isEmpty(orderDetailsBean.getOrderTypeName()) ? "-" : orderDetailsBean.getOrderTypeName());
        setText(R.id.tv_order_details_order_time,TextUtils.isEmpty(orderDetailsBean.getCreateDate()) ? "-" : orderDetailsBean.getCreateDate());
        setText(R.id.tv_order_details_order_people,TextUtils.isEmpty(orderDetailsBean.getBuyer()) ? "-" : orderDetailsBean.getBuyer());
        setText(R.id.tv_order_details_order_state,TextUtils.isEmpty(orderDetailsBean.getOrderStatusName()) ? "-" : orderDetailsBean.getOrderStatusName());
        setText(R.id.tv_order_details_order_isAllDelivery,TextUtils.isEmpty(orderDetailsBean.getIsPackage()) ? "-" : orderDetailsBean.getIsPackage().equals("1") ? "是" : "否");
        setText(R.id.tv_order_details_order_isUrgent,TextUtils.isEmpty(orderDetailsBean.getIsUrgent()) ? "-" : orderDetailsBean.getIsUrgent().equals("1") ? "是" : "否");
        setText(R.id.tv_order_details_order_delivery_time,TextUtils.isEmpty(orderDetailsBean.getDeliveryDate()) ? "-" : orderDetailsBean.getDeliveryDate());
        setText(R.id.tv_order_details_order_customer_name,TextUtils.isEmpty(orderDetailsBean.getName()) ? "-" : orderDetailsBean.getName());
        setText(R.id.tv_order_details_order_customer_phone,TextUtils.isEmpty(orderDetailsBean.getPhone()) ? "-" : orderDetailsBean.getPhone());
        setText(R.id.tv_order_details_order_community_name,TextUtils.isEmpty(orderDetailsBean.getVillage()) ? "-" : orderDetailsBean.getVillage());
        setText(R.id.tv_order_details_order_address,TextUtils.isEmpty(orderDetailsBean.getRoomNumber()) ? "-" : orderDetailsBean.getRoomNumber());
        setText(R.id.tv_order_details_order_price,textEmptyNumber(orderDetailsBean.getPrice()));
        setText(R.id.tv_order_details_order_area,TextUtils.isEmpty(orderDetailsBean.getTotalArea()) ? "-" : orderDetailsBean.getTotalArea());
        setText(R.id.tv_order_details_order_recommended_retail_price,textEmptyNumber(orderDetailsBean.getSalePrice()));
        setText(R.id.tv_order_details_order_dealer_factor,TextUtils.isEmpty(orderDetailsBean.getAccountCoefficient()) ? "-" : orderDetailsBean.getAccountCoefficient());
        setText(R.id.tv_order_details_order_amount_of_discount,TextUtils.isEmpty(orderDetailsBean.getDisAmount()) ? "-" : orderDetailsBean.getDisAmount());
        setText(R.id.tv_order_details_order_total_price_before,textEmptyNumber(orderDetailsBean.getFactoryPriceBeDis()));
        setText(R.id.tv_order_details_order_total_price_after,textEmptyNumber(orderDetailsBean.getFactoryPriceAfDis()));
        setText(R.id.tv_order_details_order_district,TextUtils.isEmpty(orderDetailsBean.getDistrictName()) ? "-" : orderDetailsBean.getDistrictName());
        contentView.findViewById(R.id.ll_order_details_order_recommended_retail_price).setVisibility(TextUtils.equals(orderDetailsBean.getIsSalePrice(), "0") ? View.GONE : View.VISIBLE);
        contentView.findViewById(R.id.ll_order_details_order_dealer_factor).setVisibility(TextUtils.equals(orderDetailsBean.getIsFactoryPrice(), "0") ? View.GONE : View.VISIBLE);
        contentView.findViewById(R.id.ll_order_details_order_total_price_before).setVisibility(TextUtils.equals(orderDetailsBean.getIsFactoryPrice(), "0") ? View.GONE : View.VISIBLE);
        contentView.findViewById(R.id.ll_order_details_order_total_price_after).setVisibility(TextUtils.equals(orderDetailsBean.getIsFactoryPrice(), "0") ? View.GONE : View.VISIBLE);

        if (!TextUtils.isEmpty(orderDetailsBean.getOrderId()))
            copy(contentView.findViewById(R.id.tv_order_details_order_number), orderDetailsBean.getOrderId());
        if (!TextUtils.isEmpty(orderDetailsBean.getPhone()))
            copy(contentView.findViewById(R.id.tv_order_details_order_customer_phone), orderDetailsBean.getPhone());

    }

    private void setText(int id, CharSequence text) {
        ((TextView) mContainer.findViewById(id)).setText(text);
    }

    @Override
    public void getDetailsFailed(String failed) {
        dismissLoading();
        mContainer.removeAllViews();
        LayoutInflater.from(mContext).inflate(R.layout.include_empty_page, mContainer, true);
        if (isNoAuth(failed)) {
            noAuthority();
        } else {
            noNetwork(failed);
        }
    }

    @Override
    protected void reload() {
        initData();
    }
}
